package com.dingreading.cloud.mall.util;

import com.mybatisflex.codegen.Generator;
import com.mybatisflex.codegen.config.ColumnConfig;
import com.mybatisflex.codegen.config.GlobalConfig;
import com.zaxxer.hikari.HikariDataSource;


public class Codegen {

    public static void main(String[] args) {
        //配置数据源
        HikariDataSource dataSource = new HikariDataSource();
//        dataSource.setJdbcUrl("jdbc:mysql://localhost/ding?characterEncoding=utf8&characterSetResults=utf8&serverTimezone=Asia/Shanghai");
//        dataSource.setUsername("root");
//        dataSource.setPassword("test2021!");
        dataSource.setJdbcUrl("jdbc:mysql://39.98.56.183:3306/ding?characterEncoding=utf8&characterSetResults=utf8&serverTimezone=Asia/Shanghai");
        dataSource.setUsername("root");
        dataSource.setPassword("VO5^b9zZWa&uZbK3");

        //创建配置内容，两种风格都可以。
        GlobalConfig globalConfig = createGlobalConfigUseStyle1();
        //GlobalConfig globalConfig = createGlobalConfigUseStyle2();
        //通过 datasource 和 globalConfig 创建代码生成器
        Generator generator = new Generator(dataSource, globalConfig);

        //生成代码
        generator.generate();
    }

    public static GlobalConfig createGlobalConfigUseStyle1() {
        //创建配置内容
        GlobalConfig globalConfig = new GlobalConfig();
        globalConfig.setSourceDir(System.getProperty("user.dir") + "/mall-service/src/main/java");
        globalConfig.setMapperXmlPath(System.getProperty("user.dir") + "/mall-service/src/main/resources/mapper");
        //设置根包
        globalConfig.setBasePackage("com.dingreading.cloud.mall");
        //设置表前缀和只生成哪些表
//        globalConfig.setTablePrefix("xy_");
        // 生成多个表
//        globalConfig.setGenerateTable("time_tasks", "app_version");
        // 生成1个表
        globalConfig.setGenerateTable("goods_category_log");
        // 生成所有表
//        globalConfig.setGenerateTable();

        //设置生成 entity 并启用 Lombok
        globalConfig.setEntityGenerateEnable(true);
        globalConfig.setEntityWithLombok(true);
        //设置生成 mapper
        globalConfig.setMapperGenerateEnable(true);
        // 服务类
        globalConfig.setServiceGenerateEnable(true);
        // 服务实现类
        globalConfig.setServiceImplGenerateEnable(true);
        // 文件
        globalConfig.setMapperXmlGenerateEnable(true);
        // 生产controller
//        globalConfig.setControllerGenerateEnable(true);

        //可以单独配置某个列
        ColumnConfig columnConfig = new ColumnConfig();
        columnConfig.setColumnName("add_time");
        columnConfig.setOnInsertValue("now()");
        columnConfig.setPropertyType("java.util.Date");
        globalConfig.setColumnConfig(columnConfig);
        ColumnConfig columnConfig2 = new ColumnConfig();
        columnConfig2.setColumnName("edit_time");
        columnConfig2.setOnInsertValue("now()");
        columnConfig2.setOnUpdateValue("now()");
        columnConfig2.setPropertyType("java.util.Date");
        globalConfig.setColumnConfig(columnConfig2);

        return globalConfig;
    }

    public static GlobalConfig createGlobalConfigUseStyle2() {
        //创建配置内容
        GlobalConfig globalConfig = new GlobalConfig();

        //设置根包
        globalConfig.getPackageConfig()
                .setBasePackage("com.test");

        //设置表前缀和只生成哪些表，setGenerateTable 未配置时，生成所有表
        globalConfig.getStrategyConfig()
                .setTablePrefix("tb_")
                .setGenerateTable("tb_account", "tb_account_session");

        //设置生成 entity 并启用 Lombok
        globalConfig.enableEntity()
                .setWithLombok(true);

        //设置生成 mapper
        globalConfig.enableMapper();

        //可以单独配置某个列
        ColumnConfig columnConfig = new ColumnConfig();
        columnConfig.setColumnName("tenant_id");
        columnConfig.setLarge(true);
        columnConfig.setVersion(true);
        globalConfig.getStrategyConfig()
                .setColumnConfig("tb_account", columnConfig);

        return globalConfig;
    }
}
